# gRPC Bazel BUILD file.
#
# Copyright 2019 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

licenses(["notice"])  # Apache v2

package(default_visibility = ["//visibility:public"])

load("//bazel:grpc_build_system.bzl", "grpc_generate_objc_one_off_targets", "grpc_objc_library")

exports_files(["LICENSE"])

grpc_generate_objc_one_off_targets()

grpc_objc_library(
    name = "rx_library_headers",
    hdrs = glob([
        "RxLibrary/*.h",
    ]),
    includes = ["."],
)

grpc_objc_library(
    name = "rx_library",
    srcs = glob([
        "RxLibrary/*.m",
    ]),
    includes = ["."],
    deps = [
        ":rx_library_headers",
        ":rx_library_private",
    ],
)

grpc_objc_library(
    name = "rx_library_private",
    srcs = glob([
        "RxLibrary/private/*.m",
    ]),
    textual_hdrs = glob([
        "RxLibrary/private/*.h",
    ]),
    visibility = ["//visibility:private"],
)

grpc_objc_library(
    name = "grpc_objc_interface_legacy",
    hdrs = [
        "GRPCClient/GRPCCall+ChannelArg.h",
        "GRPCClient/GRPCCall+ChannelCredentials.h",
        "GRPCClient/GRPCCall+Cronet.h",
        "GRPCClient/GRPCCall+OAuth2.h",
        "GRPCClient/GRPCCall+Tests.h",
        "GRPCClient/GRPCCallLegacy.h",
        "GRPCClient/GRPCTypes.h",
    ],
    deps = [
        "rx_library_headers",
    ],
)

grpc_objc_library(
    name = "grpc_objc_interface",
    srcs = [
        "GRPCClient/GRPCCall.m",
        "GRPCClient/GRPCCall+Interceptor.m",
        "GRPCClient/GRPCCallOptions.m",
        "GRPCClient/GRPCInterceptor.m",
        "GRPCClient/GRPCTransport.m",
        "GRPCClient/private/GRPCTransport+Private.m",
    ],
    hdrs = [
        "GRPCClient/GRPCCall.h",
        "GRPCClient/GRPCCall+Interceptor.h",
        "GRPCClient/GRPCCallOptions.h",
        "GRPCClient/GRPCDispatchable.h",
        "GRPCClient/GRPCInterceptor.h",
        "GRPCClient/GRPCTransport.h",
        "GRPCClient/internal/GRPCCallOptions+Internal.h",
        "GRPCClient/version.h",
    ],
    includes = ["."],
    textual_hdrs = [
        "GRPCClient/private/GRPCTransport+Private.h",
    ],
    deps = [
        ":grpc_objc_interface_legacy",
    ],
)

grpc_objc_library(
    name = "grpc_objc_client",
    srcs = [
        "GRPCClient/GRPCCall+ChannelArg.m",
        "GRPCClient/GRPCCall+ChannelCredentials.m",
        "GRPCClient/GRPCCall+Cronet.m",
        "GRPCClient/GRPCCall+OAuth2.m",
        "GRPCClient/GRPCCall+Tests.m",
        "GRPCClient/GRPCCallLegacy.m",
    ] + glob(["GRPCClient/private/GRPCCore/*.m"]),
    hdrs = [
        "GRPCClient/GRPCCall+ChannelArg.h",
        "GRPCClient/GRPCCall+ChannelCredentials.h",
        "GRPCClient/GRPCCall+Cronet.h",
        "GRPCClient/GRPCCall+OAuth2.h",
        "GRPCClient/GRPCCall+Tests.h",
    ],
    data = [":gRPCCertificates"],
    includes = ["."],
    textual_hdrs = glob(["GRPCClient/private/GRPCCore/*.h"]),
    deps = [
        ":grpc_objc_interface",
        ":grpc_objc_interface_legacy",
        ":rx_library",
        "//:grpc_objc",
    ],
)

# TODO (mxyan): Switch "name" and "actual" when import is done
# Some internal Swift projects will need to be updated with the new name
alias(
    name = "grpc_objc_client_core",
    actual = "grpc_objc_client",
)

grpc_objc_library(
    name = "proto_objc_rpc_legacy_header",
    hdrs = [
        "ProtoRPC/ProtoRPCLegacy.h",
    ],
    includes = ["."],
)

grpc_objc_library(
    name = "proto_objc_rpc_v2",
    srcs = [
        "ProtoRPC/ProtoMethod.m",
        "ProtoRPC/ProtoRPC.m",
        "ProtoRPC/ProtoService.m",
    ],
    hdrs = [
        "ProtoRPC/ProtoMethod.h",
        "ProtoRPC/ProtoRPC.h",
        "ProtoRPC/ProtoService.h",
    ],
    defines = ["GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS=0"],
    includes = ["."],
    deps = [
        ":grpc_objc_interface",
        ":proto_objc_rpc_legacy_header",
        "@com_google_protobuf//:protobuf_objc",
    ],
)

grpc_objc_library(
    name = "proto_objc_rpc",
    srcs = [
        "ProtoRPC/ProtoRPCLegacy.m",
        "ProtoRPC/ProtoServiceLegacy.m",
    ],
    hdrs = [
        "ProtoRPC/ProtoMethod.h",
        "ProtoRPC/ProtoRPC.h",
        "ProtoRPC/ProtoRPCLegacy.h",
        "ProtoRPC/ProtoService.h",
    ],
    deps = [
        ":grpc_objc_client_core",
        ":proto_objc_rpc_legacy_header",
        ":proto_objc_rpc_v2",
        ":rx_library",
        "@com_google_protobuf//:protobuf_objc",
    ],
)

load("@build_bazel_rules_apple//apple:resources.bzl", "apple_resource_bundle")

apple_resource_bundle(
    # The choice of name is signicant here, since it determines the bundle name.
    name = "gRPCCertificates",
    resources = ["//:etc/roots.pem"],
)

# Internal target combining grpc_objc_client_core and proto_objc_rpc for testing
grpc_objc_library(
    name = "grpc_objc_client_core_internal_testing",
    srcs = [
        "GRPCClient/GRPCCall+ChannelArg.m",
        "GRPCClient/GRPCCall+ChannelCredentials.m",
        "GRPCClient/GRPCCall+Cronet.m",
        "GRPCClient/GRPCCall+OAuth2.m",
        "GRPCClient/GRPCCall+Tests.m",
        "GRPCClient/GRPCCallLegacy.m",
        "GRPCClient/internal_testing/GRPCCall+InternalTests.m",
    ] + glob(["GRPCClient/private/GRPCCore/*.m"]),
    hdrs = [
        "GRPCClient/GRPCCall+ChannelArg.h",
        "GRPCClient/GRPCCall+ChannelCredentials.h",
        "GRPCClient/GRPCCall+Cronet.h",
        "GRPCClient/GRPCCall+OAuth2.h",
        "GRPCClient/GRPCCall+Tests.h",
        "GRPCClient/internal_testing/GRPCCall+InternalTests.h",
    ],
    data = [":gRPCCertificates"],
    defines = [
        "GRPC_TEST_OBJC=1",
    ],
    includes = ["."],
    textual_hdrs = glob(["GRPCClient/private/GRPCCore/*.h"]),
    deps = [
        ":grpc_objc_interface",
        ":grpc_objc_interface_legacy",
        ":rx_library",
        "//:grpc_objc",
    ],
)

grpc_objc_library(
    name = "proto_objc_rpc_internal_testing",
    srcs = [
        "ProtoRPC/ProtoRPCLegacy.m",
        "ProtoRPC/ProtoServiceLegacy.m",
    ],
    hdrs = [
        "ProtoRPC/ProtoMethod.h",
        "ProtoRPC/ProtoRPC.h",
        "ProtoRPC/ProtoRPCLegacy.h",
        "ProtoRPC/ProtoService.h",
    ],
    deps = [
        ":grpc_objc_client_core_internal_testing",
        ":proto_objc_rpc_legacy_header",
        ":proto_objc_rpc_v2",
        ":rx_library",
        "@com_google_protobuf//:protobuf_objc",
    ],
)

alias(
    name = "grpc_objc_client_internal_testing",
    actual = "proto_objc_rpc_internal_testing",
)
